package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    @GetMapping("{id}")
    public ResponseEntity<CategoryDTO>       queryCategoryById(@PathVariable ("id")Long id){
        Category byId = categoryService.getById(id);
        CategoryDTO categoryDTO = new CategoryDTO(byId);
        return ResponseEntity.ok(categoryDTO);
    }
    @GetMapping("list")
    public  ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam ("ids")List<Long> ids ){
        List<Category> categories = categoryService.listByIds(ids);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);
        return ResponseEntity.ok(categoryDTOS);
    }
    //根据父类目id查询子分类
    @GetMapping("of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
        List<Category> parent_id = categoryService.query().eq("parent_id", pid).list();
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(parent_id);
        return ResponseEntity.ok(categoryDTOS);
    }
    @GetMapping("of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrand(@RequestParam("id") Long brandId){
        List<CategoryDTO> categoryDTOS = categoryService.queryCategoryByBrandId(brandId);
        return ResponseEntity.ok(categoryDTOS);
    }
}
